package com.buddy.qreport.query.dao;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.query.entity.QueryEntity;
import com.buddy.qreport.query.params.QueryParam;
import com.buddy.qreport.query.result.QueryResultColumn;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;
import java.util.Map;

@Mapper
public interface IQueryMapper {



    @Insert({"<script>",

            "insert into t_query_param(param_id,param_name,param_datasource_type,param_data_type,param_datasource_content,param_query,param_label)",
            "values",
            "<foreach collection =\"list\" item=\"item\" index= \"index\" separator =\",\">",

            "((select replace(uuid(),'-','')) ,#{item.paramName},#{item.paramDatasourceType},#{item.paramDataType},#{item.paramDatasourceContent},#{queryId},#{item.paramLabel})",

            "</foreach>",

            "</script>"
    })
    void insertParamsBatch(@Param("list") List<QueryParam> params,@Param("queryId") String queryId);

    @Delete("delete from t_query_param where param_query=#{id}")
    void deleteParams(String queryId);


    @Select("select * from t_query_param where param_query = #{id}")
    List<QueryParam> selectParams(String queryId);

    @Insert({"<script>",

            "insert into t_query_result_column(column_id,column_field,column_data_type,column_query,column_desc)",
            "values",
            "<foreach collection =\"list\" item=\"item\" index= \"index\" separator =\",\">",

            "((select replace(uuid(),'-','')) ,#{item.columnField},#{item.columnDataType},#{queryId},#{item.columnDesc})",

            "</foreach>",

            "</script>"
    })
    void insertResultColumnBatch(@Param("list") List<QueryResultColumn> resultColumns,@Param("queryId") String queryId);


    @Delete("delete from t_query_result_column where column_query = #{id}")
    void deleteColumns(String queryId);

    @Select("select * from t_query_result_column where column_query = #{id}")
    List<QueryResultColumn> selectColumns(String queryId);

    @Select("select * from t_query_result_column where column_id = #{id}")
    QueryResultColumn selectOneColumn(String columnId);

    @Select("${sql}")
    List<Map> selectDynamicSql(@Param("sql") String sql);

    @Select("${sql}")
    List<Map> dynamicSelect(Map map);

    @Update({"update t_query set",
            "query_name=#{queryName},query_createtime=#{queryCreatetime},query_lastexectime=#{queryLastexectime}, query_datasource=#{queryDatasource},query_sql=#{querySql},",
            "query_url = #{queryUrl},query_type=#{queryType}},query_method=#{queryMethod}",
            "where query_id=#{queryId}"
    })
    void updateById(QueryEntity entity);


    @Update("update t_query set quert_lastexectime = (UNIX_TIMESTAMP(now())*1000) where query_id = #{id}")
    void updateLastExecTime(String id);

    @SelectKey(statementType = StatementType.PREPARED, resultType = String.class,keyProperty = "queryId",statement = "select replace(uuid(),'-','') from dual ",before = true)
    @Insert({"insert into t_query (query_id,query_name,query_datasource,query_sql,query_createtime,query_lastexectime,query_url,query_type,query_method)",

            "values(#{queryId},#{queryName},#{queryDatasource},#{querySql},(UNIX_TIMESTAMP(now())*1000),0,#{queryUrl},#{queryType},#{queryMethod})"

    })
    void insert(QueryEntity entity);

    @Delete("delete from t_query where query_id= #{id}")
    void delete(String id);


    @Select("select * from t_query")
    List<QueryEntity> selectList(Page page);

    @Select("select * from t_query where query_id = #{id}")
    QueryEntity selectOne(String id);
}
